/**
 * Created by fengt on 2015/10/14.
 */
//----------->>>全局变量>>>------------

//-----------<<<全局变量<<<------------
$(function(){

    paging(1);
    /*//loading页面配置
    //$("#loading").find("img").css({"top":"50%","left":"50%","margin-top":"-16px","margin-left":"-16px"});
    //$("#loading").find("div").css({"top":"50%","left":"50%","margin-top":"16px","margin-left":"-50px"});
    //1 绑定资源编辑按钮
    resManageBtn1();
    //2 策略按钮
    strategyManagerBtn();
    //3 基本信息
    baseInfoBtn();
    //4采集指标
    collectManageBtn();
    //5 配置按钮
    configureBtn();
    //6 第三方联系人
    contactsBtn();*/
  
});
//--------------<<<绑定事件<<<-----------
$("body").on("click",".configureBtn",resConfigure);
$("body").on("click",".collectBtn",collectIndicator);
$("body").on("click",".strategyBtn",strategyManager);
$("body").on("click",".contactsBtn",contactsBtn)
//-------------->>>绑定事件>>>-----------
/**
 * 分页获取资源
 * @param pageNo
 */
function paging(pageNo) {
    pageNo = pageNo==null?1:pageNo;
    var pageSize=$("#pageSize option:selected").text();
    pageSize=pageSize==null?10:pageSize;
    $.ajax({
        url: basePath+"/radar/resource/pagingResource.do",
        type: "post",
        dataType: 'json',
        async: false,
        data: {
            pageNo:pageNo,
            pageSize:pageSize,
        },
        error: function (data) {
            console.log(data);
            layer.msg(data.message,{time:2000});
        },
        success: function (jsons) {
            if(jsons.code == 500){
                layer.msg(jsons.errorMessage);
            }else if (jsons.code == 201){
                layer.msg("数据为空！");
            }else{
                unifiedPaging(jsons.pageData,"radarResource");
            }
        }
    });
}
/**
 * 配置按钮 获取配置信息
 */
function resConfigure(){
    var resourceId=$(this).parents("tr").find("input[type='checkbox']").eq(0).val();
    if(resourceId==null||resourceId==""){
        layer.msg('参数错误',{time:2000})
    }else{
       layer.open({
            type:2,
            title:'配置信息',
            area:['800px','500px'],
            content:basePath+"/cmdb/resource/initEditResource.do?resourceId="+resourceId,
            maxmin: true,
            shadeClose: true
        });
    }

}
/**
 * 点击采集指标按钮
 */
function collectIndicator(){
    var resourceId=$(this).parents("tr").find("input[type='checkbox']").eq(0).val();
    layer.open({
        type:2,
        title:'采集指标',
        area:['800px','400px'],
        content:basePath+"/radar/resource/initCollectIndicator.do?resourceId="+resourceId,
        maxmin: true,
        shadeClose: true
    });
}


/**
 * 联系人组
 */
function contactsBtn(){
    var resourceId=$(this).parents("tr").find("input[type='checkbox']").eq(0).val();
    layer.open({
        type:2,
        title:'联系人组',
        area:['800px','500px'],
        content : basePath + "/radar/linkGroup/resourceLinkGroupView.do?resourceId="+resourceId,
        maxmin: true,
        shadeClose: true
    });
}


function strategyManager(){
    var parentTr=$(this).parent().parent().parent().parent().parent();

    var resourceId = $(parentTr).attr('id');
    var classes = $(this).attr("classes");
    window.location.href=basePath+"/radar/strategy/resourceAndStrategy.do?resourceId="+resourceId+"&resourceType="+classes;
}

/**
 * 点击资源管理按钮
 */
resManageBtn=function(){
    $(".resManageBtn").click(function(){
        var parentTr=$(this).parent().parent().parent().parent().parent();
        var resource_id = $(parentTr).attr('id');
        //$.getJSON("selectResbyId.do",{'resource_id':resource_id},function(result){
        var path =$("input[name=path]").val();
        $.ajax2(path,{
            url: "selectResbyId.do",
            type: "post",
            dataType: 'json',
            data: {
                'resource_id': resource_id
            },
            success: function (result) {
                layer.config({
                    extend: ['skin/layer_iframe.css'],
                    skin: 'layer-ext-skin-layer_iframe.css'
                });
                index = layer.open({
                    type: 1,
                    title: '凭证管理',
                    area: ['800px', '400px'],
                    content: handlerTemplate(result.data, "resManage-template"),
                    maxmin: true,
                    shadeClose: true
                });
                //注册保存按钮
                $("#saveBtn").click(function () {
                    var data= {};
                    data['resourceId']=$("input[name=resourceId]").val();
                    data['uuid']=$("input[name=uuid]").val();
                    data['manageIp']=$("input[name=manage_ip]").val();
                    data['protocol']=$("select[name=protocol]  option:selected").val();
                    data['platformId']=$("select[name=platformId]  option:selected").val();
                    if($("input[name=isping]").attr('checked') =="checked"){
                        data['isping']=true;
                    }else{
                        data['isping']=false;
                    }
                    //sysEcho($("input[name=issyslog]").attr('checked'));
                    if($("input[name=issyslog]").attr('checked') =="checked"){
                        data['issyslog']=true;
                    }else{
                        data['issyslog']=false;
                    }
                    data['account']=$("input[name=account]").val();
                    data['password']=$("input[name=password]").val();
                    data['connectTimeout']=$("input[name=connect_timeout]").val();
                    data['executeTimeout']=$("input[name=execute_timeout]").val();
                    data['interactPrompt']=$("input[name=interact_prompt]").val();
                    data['collectorIp']=$("input[name=collector_ip]").val();

                    $.ajax2(path,{
                        url: "editRes.do",
                        type: "post",
                        dataType: 'json',
                        data: data,
                        success: function (result) {
                            if (result.errorMessage==200) {
                                layer.msg('修改成功', {time: 2000});
                                paging(1);
                                layer.closeAll();
                            } else {
                                $("#tipMsg").html("");
                                layer.msg(result.errorMessage, {time: 2000});
                            }
                        }
                    },".layui-layer");
                });
            }
        },".layui-layer");
    });
}

resManageBtn1=function(){
    $(".resManageBtn").click(function(){
        var parentTr=$(this).parents("tr");
        var resource_id = $(parentTr).attr('id');
        //$.getJSON("selectResbyId.do",{'resource_id':resource_id},function(result){
        var path =$("input[name=path]").val();
        //layer.config({
        //    extend: ['skin/layer_iframe.css'],
        //    skin: 'layer-ext-skin-layer_iframe.css'
        //});
        //index = layer.open({
        //    type: 2,
        //    title: '凭证管理',
        //    area: ['900px', '400px'],
        //    content: "editResource.do?resourceId="+resource_id,
        //    maxmin: true,
        //    shadeClose: true
        //});

        layer.open({
            type:2,
            title: '凭证管理',
            area:['800px','400px'],
            extend:['skin/layer_iframe.css'],
            skin:'layer-ext-skin-layer_iframe.css',
            content:path+'/hhm/monitorresource/editResource.do?resourceId='+resource_id,
            maxmin: true,
            shadeClose: true
        });
    });
}
queryInstances=function(pageNo,pageindex,order){
    paging(pageindex);
}


/**
 * 模版处理方法
 * @param data
 * @returns {*}
 */
handlerTemplate=function(data,node){
    //注册一个Handlebars模版，通过id找到某一个模版，获取模版的html框架

    var template = Handlebars.compile($("#"+node).html());
    //注册一个翻译用的Helper，0翻译成男，1翻译成女
    Handlebars.registerHelper("transformat",function(value){
        //sysEcho(value);
        if(value==false){
            return "否";
        }else if(value==true){
            return "是";
        }
    });
    //
    Handlebars.registerHelper("transformatimg",function(value){
        //sysEcho(value);
        if(value==false){
            return '<span class="icon01" ></span>';
        }else if(value==true){
            return '<span class="icon04" ></span>';
        }
    });
    //时间戳转换时间格式
    Handlebars.registerHelper("prettifyDate", function(timestamp) {
        //format("yyyy-MM-dd HH:mm:ss");
        return (new Date(parseInt(timestamp))).toLocaleString().substr(0,17);
    });
    /**
     * 处理select option选择
     */
    window.Handlebars.registerHelper('select', function( value, options ){
        //sysEcho(value);
        var $el = $('<select />').html( options.fn(this) );
        $el.find('[value="' + value + '"]').attr({'selected':'selected'});
        return $el.html();
    });
    /**
     * 处理isping issyslog 勾选
     */
    Handlebars.registerHelper("selectcheckbox", function(value) {
        //sysEcho(value);
        var result="";
        if(value===true){
            result="checked";
        }
        return result
    });
    return template(data);
}
